package collector;

import console.Logger;
import parser.JavaRecognizer;
import parser.JavaTokenTypes;

import java.util.HashMap;
import java.util.Map;

public class Configuration {

    // Singleton workaround for now
    private static Configuration instance;
    private boolean dumpAst;

    public void info(final String s) {
        if (logger == null) {
            System.out.println(s);
            System.out.flush();
        } else {
            logger.info(s);
        }
    }

    public void error(final String s) {
        if (logger == null) {
            System.err.println(s);
            System.err.flush();
        } else {
            logger.error(s);
        }
    }

    private Logger logger;

//    public int getMinLengthForChunk() {
//        return minLengthForChunk;
//    }

    public void setMinLengthForChunk(final int minLengthForChunk) {
        this.minLengthForChunk = minLengthForChunk;
    }

    public int minLengthForChunk = 4;

    private HashMap<Integer, Boolean> equateTable = new HashMap<Integer, Boolean>();
    static final String EOL = System.getProperty("line.separator");


    //for debug:
    public boolean openGui;

    //thread option
//    public static final int THREAD_POOL_SIZE = 2;
    //public static boolean threadCollect = false;

    //stuff
    public static boolean s_fullChunkInfo = false;

    public Configuration() {
        this(false);
    }

    public Configuration(boolean openGui) {
        this.openGui = openGui;
        equateTable.put(JavaTokenTypes.TYPE, false);
        equateTable.put(JavaTokenTypes.IDENT, false);
        equateTable.put(JavaTokenTypes.LITERAL, false);
        instance = this;
    }

    public boolean anonymizeType(int type) {
        return equateTable.containsKey(type)
                && equateTable.get(type);
    }

    public static Configuration getInstance() {
        return instance;
    }

    public void setDumpAst(final boolean b) {
        dumpAst = b;
    }

    public boolean isDumpAst() {
        return dumpAst;
    }

    public void setAnonymizeTypes(final boolean value) {
        equateTable.put(JavaTokenTypes.TYPE, value);
    }

    public void setAnonymizeVariables(final boolean value) {
        equateTable.put(JavaTokenTypes.IDENT, value);
    }

    public String getAnonymized() {
        StringBuffer tmp = new StringBuffer();
        for (Map.Entry<Integer, Boolean> entry : equateTable.entrySet()) {
            if (entry.getValue()) {
                String item = JavaRecognizer._tokenNames[entry.getKey()];
                if (tmp.length() > 0)
                    tmp.append(", ");
                tmp.append(item);
            }
        }
        return tmp.toString().intern();
    }

    public Map<Integer, Boolean> getEquateTable() {
        return equateTable;
    }

    public boolean isGui() {
        return openGui;
    }

    public void setLogger(final Logger logger) {
        this.logger = logger;
    }

    public void setAnonymizeLiterals(final boolean b) {
        equateTable.put(JavaTokenTypes.LITERAL, b);
    }
}
